package pl.darbat.photogallery;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import pl.darbat.photogallery.Debug.MemoryStatusListener;
import pl.darbat.photogallery.html.HtmlParser;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;

public class CategoriesActivity extends Activity implements OnItemClickListener, MemoryStatusListener {
	
	private static final String TAG = "CategoriesActivity";
	private String[] mCategoryName;
	private String[] mCategoryLink;
	private AdView mAdView; 
	private ListView mListView;
	
	private SimpleAdapter mCategoryAdapter;
	
	private String[] mDataType = new String[] { "name", "image", "link"};
	private int[] mInsertTo = new int[] { R.id.categoryName, R.id.categoryImage, R.id.categoryLink};
	private ArrayList<Map<String, Object>> mCategoryList;
	private TextView mTitleView;
	private Thread mMemoryStatus;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView) findViewById(R.id.categoryList);
        
        mCategoryName = getResources().getStringArray(R.array.categoryName);
        mCategoryLink = getResources().getStringArray(R.array.categoryLink);
        mCategoryList = new ArrayList<Map<String, Object>>();
        for(int i = 0; i < mCategoryName.length; i++){
        	HashMap<String, Object> categoryRow = new HashMap<String, Object>();
        	categoryRow.put("name", mCategoryName[i]);
        	categoryRow.put("image", R.drawable.icon);
        	categoryRow.put("link", mCategoryLink[i]);
        	mCategoryList.add(categoryRow);
        }
        mCategoryAdapter = new SimpleAdapter(getApplicationContext(), mCategoryList, R.layout.category_row, mDataType, mInsertTo);
        
        mListView.setOnItemClickListener(this);
        mListView.setAdapter(mCategoryAdapter);
    	mAdView = (AdView) findViewById(R.id.categoryAd);
    	
        if(getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
        	if(Debug.CATEGORIESACTIVITY) Log.v(TAG,"Orientation portrait or square.");
        	AdManager.setTestDevices( new String[] { "45DE800D9F53A86F450631C6684BE921" } );      
        	AdManager.setAllowUseOfLocation(true); 
        	mAdView.setEnabled(true);
        	//mAdView.requestFreshAd();
        } else {
        	mAdView.setEnabled(false);
        }
		mTitleView = (TextView) findViewById(R.id.categoryTitleBar);
		mMemoryStatus = Debug.createMemoryStatusThread(this);
		mMemoryStatus.start();
    }

	public void onItemClick(AdapterView parent, View v, int position, long id) {
		if(Debug.CATEGORIESACTIVITY) Log.v(TAG,"onItemClick");

		Intent intent = new Intent(this, CategoryGridActivity.class);
		intent.putExtra("categoryLink", (String) mCategoryList.get(position).get("link"));
		startActivity(intent);
		
		//Open Browser:
		//String url = (String) mCategoryList.get(position).get("link");
		//Intent i = new Intent(Intent.ACTION_VIEW);
		//i.setData(Uri.parse(url));
		//startActivity(intent);
	}
	
	public void onMemoryStatus(final String status) {
		runOnUiThread(new Runnable(){

			public void run() {
				mTitleView.setText(status);
			}
		});
	}
}